'''
测试接口:动物生产单位/保存
作者:王鑫
'''
import json
import time
from unittest import TestCase

from jsonpath import jsonpath
from parameterized import parameterized

from utils.reader import Reader
from utils.session import Session


class ProductionUnitSaveTest(TestCase):
    '''
    动物生产单位保存接口测试类
    作者:王鑫
    '''

    #共享会话
    session = None
    url = "http://120.53.243.26:8009/api/sysOperateLog/findPage"
    #header存放共享会话的token
    header = None
    num = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.session = Session.get_session()
        cls.header = Session.get_header()
        cls.num = str(int(time.time()*10))


    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    @parameterized.expand(Reader.read_excel('wx.xlsx', min_row=16, max_row=17, min_col=9, max_col=12))
    def test_sys_operate_log_find_page1(self, data, expcted_status_code, verify_point, expected_value):
        data = json.loads(data)
        data['supplierCode'] = data['supplierCode'] + self.num
        data['supplier'] = data['supplier'] + self.num

        # 发送请求
        resp = self.session.post(url=self.url, headers=self.header, json=data)

        body = resp.json()

        # 根据验证点提取出实际数据
        res_code = jsonpath(body, f"$..{verify_point}")
        res_code = res_code[0]

        # 断言检验响应状态码
        self.assertEqual(expcted_status_code, resp.status_code)
        # 断言检验响应正文的验证点
        if isinstance(expected_value, int):
            self.assertEqual(0, res_code)
        else:
            self.assertNotEqual(0,res_code)

    @parameterized.expand(Reader.read_excel('wx.xlsx', min_row=18, max_row=23, min_col=9, max_col=12))
    def test_sys_operate_log_find_page2(self, data, expcted_status_code, verify_point, expected_value):
        data = json.loads(data)
        time.sleep(0.1)
        strs = str(int(time.time() * 10))
        data['supplierCode'] = data['supplierCode'] + strs
        data['supplier'] = data['supplier'] + strs

        # 发送请求
        resp = self.session.post(url=self.url, headers=self.header, json=data)

        body = resp.json()

        # 根据验证点提取出实际数据
        res_code = jsonpath(body, f"$..{verify_point}")
        res_code = res_code[0]

        # 断言检验响应状态码
        self.assertEqual(expcted_status_code, resp.status_code)

        # 断言检验响应正文的验证点
        if isinstance(expected_value, int):
            self.assertEqual(0, res_code)
        else:
            self.assertNotEqual(0, res_code)


    @parameterized.expand(Reader.read_excel('wx.xlsx', min_row=24, max_row=27, min_col=9, max_col=12))
    def test_sys_operate_log_find_page3(self, data, expcted_status_code, verify_point, expected_value):
        data = json.loads(data)
        resp = self.session.post(url=self.url, headers=self.header, json=data)
        body = resp.json()

        # 根据验证点提取出实际数据
        res_code = jsonpath(body, f"$..{verify_point}")
        res_code = res_code[0]

        # 断言检验响应状态码
        self.assertEqual(expcted_status_code, resp.status_code)

        # 断言检验响应正文的验证点
        if isinstance(expected_value, int):
            self.assertEqual(0, res_code)
        else:
            self.assertNotEqual(0, res_code)

